<#include "/macro.include"/>
<#include "/java_copyright.include">
<#assign className = table.className>   
<#assign classNameLower = className?uncap_first>
package ${basepackage}.controller;
import ${basepackage}.model.${className};
import ${basepackage}.service.${className}Service;
import ${basepackage}.controller.api.${className}ServiceApi;
import ${basepackage}.controller.api.dto.${className}Dto;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

<#include "/java_imports.include">
@Api(value = "${classNameLower}Service", tags = "${table.remarks}", produces = MediaType.APPLICATION_JSON_VALUE)
@Slf4j
@RestController
public class ${className}Controller implements ${className}ServiceApi {

	@Autowired
	private ${className}Service ${classNameLower}Service;
	
	/**  
     *   
     * <save one>  @PostMapping feign可以不写
     * @param ${classNameLower}Dto
     * @return ResultDTO<Integer>
     */
	@PostMapping
	public ResultDTO<Integer> insert${className}(@RequestBody @Valid ${className}Dto ${classNameLower}Dto){
		ResultDTO<Integer> result= new ResultDTO<Integer>();
		${className} ${classNameLower} = null;
		try{
			${classNameLower}=BeanMapper.map(${classNameLower}Dto,${className}.class);
			${classNameLower}Service.insert(${classNameLower});
			result.setData(${classNameLower}.getId());
		}catch (Exception e) {
			result.setStatus(MessageStatus.SUCCESS.getValue());
			//result.setMessage(MessageCode.SUCCESS.getMessage());
			//result.setCode(MessageCode.SUCCESS.getCode());
			if(log.isErrorEnabled()){
				log.error("${className}Service.insert${className}(${className}Dto ${classNameLower}Dto) error\n", e);
			}
		}
		return result;
	}
	 /**  
     *   
     * <update one>    @PutMapping feign可以不写
     * @param ${classNameLower}Dto
     * @return ResultDTO<Integer>  
     */
	 @PutMapping
	 public ResultDTO<Integer> update${className}(@RequestBody @Valid ${className}Dto ${classNameLower}Dto){
		ResultDTO<Integer> result= new ResultDTO<Integer>();
		int u=0;
		try{
			${className} ${classNameLower}=BeanMapper.map(${classNameLower}Dto,${className}.class);
			u=${classNameLower}Service.update(${classNameLower});
			result.setData(u);
		}catch (Exception e) {
			result.setStatus(MessageStatus.SUCCESS.getValue());
			//result.setMessage(MessageCode.SUCCESS.getMessage());
			//result.setCode(MessageCode.SUCCESS.getCode());
			if(log.isErrorEnabled()){
				log.error("${className}Service.update${className}(${className}Dto ${classNameLower}Dto) error\n", e);
			}
		}
		return result;
	}

	/**
	 *
	 * <find one by id> @GetMapping和@RequestParam可以不写
	 * @param id
	 * @return ResultDTO<${className}Dto>
	 */
	@GetMapping(value = "/get${className}ById")
	ResultDTO<${className}Dto> get${className}ById(@RequestParam("id") ${table.idColumn.simpleJavaType} id){
	 	return this.get${className}ById(id);
	}

	/**
	*
	* <find one by id>
	* @param id
	* @return ResultDTO<${className}Dto>
	*/
	@GetMapping(value = "/{id}")
	public	ResultDTO<${className}Dto> get${className}ById(@PathVariable(value="id") ${table.idColumn.simpleJavaType} id){
		ResultDTO<${className}Dto> result= new ResultDTO<${className}Dto>();
		${className} ${classNameLower}=null;
		try{			
			${classNameLower}=${classNameLower}Service.get(id);
			${className}Dto ${classNameLower}Dto=BeanMapper.map(${classNameLower},${className}Dto.class);
			result.setData(${classNameLower}Dto);
		}catch (Exception e) {
			result.setStatus(MessageStatus.SUCCESS.getValue());
			//result.setMessage(MessageCode.SUCCESS.getMessage());
			//result.setCode(MessageCode.SUCCESS.getCode());
			if(log.isErrorEnabled()){
				log.error("${className}Service.get${className}ById(${table.idColumn.simpleJavaType} id) error\n", e);
			}
		}	
		return result;
	}
	 /**  
     *   
     * <query all>  
     * @param pageParsDTO 
     * @return ResultDTO<List<${className}Dto>>
     */
	public ResultDTO<PageResultDTO<${className}Dto>> search${className}(@RequestBody PageParsDTO<Map> pageParsDTO){
		ResultDTO<PageResultDTO<${className}Dto>> result= new ResultDTO<PageResultDTO<${className}Dto>>();
		List<${className}> ${classNameLower}List = null;
		try{
			${classNameLower}List=${classNameLower}Service.search(pageParsDTO.getParam(),new PageBounds(pageParsDTO.getPage(), pageParsDTO.getLimit(),  Order.formString(pageParsDTO.getSort())));
			PageResultDTO<${className}Dto> pageResultDTO=PaginatorUtil.mapList(${classNameLower}List,${className}Dto.class);
			result.setData(pageResultDTO);
		}catch (Exception e) {
			result.setStatus(MessageStatus.SUCCESS.getValue());
			//result.setMessage(MessageCode.SUCCESS.getMessage());
			//result.setCode(MessageCode.SUCCESS.getCode());
			if(log.isErrorEnabled()){
				log.error("${className}Service.search${className}(PageParsDTO<Map> pageParsDTO) error\n", e);
			}
		}	
		return result;
	}
}
